<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>骡窝日报详情</title>
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script type="text/javascript" src="js/review/js/jquery.comment.js" ></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
		<link rel="stylesheet" href="/css/reset.css">
		<link rel="stylesheet" href="/css/newsContent.css">
		<script src="/js/plugins/jrender/jrender.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
		<script src="/js/plugins/dialog/dialog.min.js"></script>
		<script src="/js/common.js"></script>
		<style>
			.container{
				width: 100%;
			}
			.commentbox{
				width: 900px;
				margin: 20px auto;
			}
			.mytextarea {
				width: 100%;
				overflow: auto;
				word-break: break-all;
				height: 100px;
				color: #000;
				font-size: 1em;
				resize: none;
			}
			.comment-list{
				width: 900px;
				margin: 20px auto;
				clear: both;
				padding-top: 20px;
			}
			.comment-list .comment-info{
				position: relative;
				margin-bottom: 20px;
				margin-bottom: 20px;
				border-bottom: 1px solid #ccc;
			}
			.comment-list .comment-info header{
				width: 10%;
				position: absolute;
			}
			.comment-list .comment-info header img{
				width: 100%;
				border-radius: 50%;
				padding: 5px;
			}
			.comment-list .comment-info .comment-right{
				padding:5px 0px 5px 11%;
			}
			.comment-list .comment-info .comment-right h3{
				margin: 5px 0px;
			}
			.comment-list .comment-info .comment-right .comment-content-header{
				height: 25px;
			}
			.comment-list .comment-info .comment-right .comment-content-header span,.comment-list .comment-info .comment-right .comment-content-footer span{
				padding-right: 2em;
				color: #aaa;
			}
			.comment-list .comment-info .comment-right .comment-content-header span,.comment-list .comment-info .comment-right .comment-content-footer span.reply-btn,.send,.reply-list-btn{
				cursor: pointer;
			}
			.comment-list .comment-info .comment-right .reply-list {
				border-left: 3px solid #ccc;
				padding-left: 7px;
			}
			.comment-list .comment-info .comment-right .reply-list .reply{
				border-bottom: 1px dashed #ccc;
			}
			.comment-list .comment-info .comment-right .reply-list .reply div span{
				padding-left: 10px;
			}
			.comment-list .comment-info .comment-right .reply-list .reply p span{
				padding-right: 2em;
				color: #aaa;
			}
		</style>

	</head>
	<script>
        var user = JSON.parse(sessionStorage.getItem("user"));
        $(function () {
            //拿到文章的id
            var id = getParams().id;
            console.log(id);
            //发送ajax请求查询文章信息
            $.get("/news/"+id, function (data) {
                $(".newsCommends").renderValues(data);
            });


                //显示相关推荐
                $.get("/news", {state: 1}, function (data) {
                    $("#carouselExampleControls").renderValues(data, {
                        handle: function (ele, value) {
                            ele.href = "/newsContent.html?id="+value;
                        }
                    });
                    $("#newsCommends > div:first").addClass("active");
                }, "json");


            //-------------------------------------------------------- 评论
            //初始化数据
            var author;
            var pages;
            var currentPage = 1;
            var travels; //游记数组
            var arr = [];
            $.get("/reviews" , {"state":3,"essayId":id},function (data) {
                console.log(data.list);
                pages=data.pages;
                arr = data.list;
                travels = arr;
                $(".comment-list").addCommentList({data:arr,add:""});
            });

            //当屏幕滚到最底部时执行函数
            $(window).scroll(function () {
                //判断是否到底部
                if (isEnd()) {
                    //判断是否有下一页
                    if (currentPage < pages) {
                        //设置当前页+1,并且发送ajax请求获取下一页数据
                        console.log(pages);
                        currentPage++;
                        $.get("/reviews", {currentPage: currentPage, state:3, essayId:id}, function (data) {
                            arr = data.list;
                            $(".comment-list").addCommentList({data:arr,add:""});
                        });
                    } else {
                        $(document).dialog({
                            type : "notice",
                            infoText: "已经到底部了",
                            autoClose: 1500,
                            position: "center"
                        });
                    }
                }
            });

            $("#comment").click(function(){
                if (user) {
                    var review =$("#content").val();
                    if (review) {
                        var json = {"user.id": user.id, "authorId": 0, "state": 3, "essayId": id, "review": review};
                        $.post("/reviews", json, function (data) {
                            window.location.reload();
                        })
                    }else {
                        $(document).dialog({
                            type: 'confirm',
                            style: 'IOS',
                            titleText: "提示",
                            content: "评论不能为空!",
                            buttons: [
                                {
                                    name: '确定',
                                    callback: function () {
                                    }
                                },
                            ]
                        });
					}
                }else {
					login();
                }
            });
            //-------------------------------------------------------------------------------
        });

        function replyClick(el){
            el.parent().parent().append("<div class='replybox'><textarea cols='80' rows='50' placeholder='来说几句吧......' class='mytextarea' ></textarea><span class='send'>发送</span></div>")
                .find(".send").click(function(){
                var content = $(this).prev().val();
                if (!user){
                    login();
				}
                if(content != ""){
                    var parentEl = $(this).parent().parent().parent().parent();
                    var beReplyName = parentEl.find("h3").text();
                    if (beReplyName === user.nickName){
                        $(document).dialog({
                            type : 'confirm',
                            style: 'IOS',
                            titleText: "提示",
                            content: "请不要回复自己!",
                            buttons: [
                                {
                                    name: '确定',
                                    callback: function() {
                                        window.location.reload();
                                    }
                                },
                            ]
                        });
                    }else {
                        var replyId = user.id;//当前回复评论的用户
                        var beReplyId = parentEl.find("h3").data("uid");//该评论的用户
                        var reviewId = parentEl.find("h3").data("reviewid");
                        var img = user.headImgUrl;
                        var json = {"img":img,"replyId":replyId, "reviewId":reviewId, "content":content};
                        $.post("/reviews/"+beReplyId+"/replies",json , function (data) {
                            window.location.reload();
                        })
                    }
                }else{
                    alert("空内容");
                }
            });
        }

        function login() {
            $(document).dialog({
                type: "confirm",
                style: "IOS",
                titleShow: false,
                content: "请登录",
                buttons: [{
                    name: "跳转到登录界面",
                    callback: function () {
                        location.href = "/login.html";
                    }
                }]
            });
            return;
        }
	</script>

<body>
	<div class="newsDetail">
		<div class="newsCommends">
			<a href="javascript:history.go(-1);" style="position: absolute;top: 10px;left: 22px;">
				<span>
					<i class="fa fa-chevron-left fa-2x" style="color: white;"></i>
				</span>
			</a>
			<img render-src="coverUrl">
			<div class="container">
				<div class="row typeRow">
					<div class="col-4">
						<span class="type" render-html="typeName"></span>
					</div>
					<div class="col-2">
					</div>
					<div class="col-2">
						<span>By</span>
					</div>
					<div class="col-4">
						<span class="type">骡窝官方</span>
					</div>
				</div>

				<div class="detail">
					<h2 class="title" render-html="title"></h2>
					<div class="content" render-html="newContent.content">
				</div>
			</div>
		 </div>
		</div>
		<div class="container">
			<h2>相关推荐</h2>
			<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
				<div id="newsCommends" class="carousel-inner" render-loop="list">
					<div class="carousel-item">
						<div class="border commend">
							<a href="travelContent.html" render-key="list.id" render-fun="handle">
								<img render-src="list.coverUrl" width="10%" height="10px">
								<h6 render-html="list.title"></h6>
								<h6 render-html="list.releaseTime"></h6>
							</a>
						</div>
					</div>
				</div>

				<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
					<span class="carousel-control-prev-icon" aria-hidden="true"></span>
					<span class="sr-only"></span>
				</a>
				<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
					<span class="carousel-control-next-icon" aria-hidden="true"></span>
					<span class="sr-only"></span>
				</a>
			</div>
		</div>
		<br/>
		<!-- 评价部分开始 -->
		<div class="container">
			<div class="commentbox">
				<textarea cols="80" rows="50" placeholder="来说几句吧......" class="mytextarea" id="content"></textarea>
			</div>
			<div class="btn btn-info pull-right" id="comment">评论</div>
			<div class="comment-list">

			</div>
		</div>
	</div>
	<!-- ---------------------------评论----------------------- -->
</body>

</html>
